package com.woniu.darryringmall.front.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.darryringmall.front.entity.Goods;
import com.woniu.darryringmall.front.entity.GoodsSpec;
import com.woniu.darryringmall.front.entity.SpecValue;
import com.woniu.darryringmall.front.entity.ViewGoods;
import com.woniu.darryringmall.front.mapper.GoodsSpecMapper;
import com.woniu.darryringmall.front.mapper.ImagesMapper;
import com.woniu.darryringmall.front.mapper.ViewGoodsMapper;
import com.woniu.darryringmall.front.service.ViewGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实现商品接口的类
 */
@Service
public class ViewGoodsServiceImpl extends ServiceImpl<ViewGoodsMapper, ViewGoods> implements ViewGoodsService {

    @Autowired
    private ViewGoodsMapper viewGoodsMapper;
    @Autowired
    private GoodsSpecMapper goodsSpecMapper;
    @Autowired
    private ImagesMapper imagesMapper;

    /**
     * 商品详情展示
     * @param gid
     * @return
     */
    @Override
    public ViewGoods view(int gid) {
        ViewGoods viewGoods = viewGoodsMapper.getByPrice(gid).get(0);
        JSONObject defaultSpec = JSONObject.parseObject(viewGoods.getSpecValues());
        viewGoods.setDefaultSpec((String)defaultSpec.get("spec"));
        viewGoods.setDefaultPurity((String)defaultSpec.get("purity"));
        List<Integer> arrayList1=new ArrayList();
        for (int i = 15; i < 28; i++) {
            arrayList1.add(i);
        }
        viewGoods.setSize(arrayList1);

        List<BigDecimal> arrayList2=new ArrayList();
        for ( double i = 10; i < 20; i++) {
            arrayList2.add(BigDecimal.valueOf((i/15)).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        viewGoods.setWeight(arrayList2);
        Map<String,List<String>> map= change(viewGoods,gid);
        viewGoods.setPurities(map.get(defaultSpec.get("spec")));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("goodsId",gid);
        viewGoods.setImagesList(imagesMapper.selectList(queryWrapper));

        return viewGoods;

    }

    public Map<String,List<String>> change(ViewGoods viewGoods,int gid){
        Map<String,List<String>> map= new HashMap<>();
        ArrayList specList=new ArrayList();
        for (String sp:goodsSpecMapper.getSpecValue(gid)) {
            JSONObject spec = JSONObject.parseObject(sp);
            if(map.get(spec.get("spec"))== null){
                ArrayList arrayList =  new ArrayList();
                arrayList.add(spec.get("purity"));
                map.put((String)spec.get("spec"),arrayList);
                specList.add(spec.get("spec"));
            }else{
                List<String> list= map.get(spec.get("spec"));
                list.add((String)spec.get("purity"));
                map.put((String)spec.get("spec"),list);
            }
        }
        viewGoods.setSpecs(specList);
        return map;
    }

    @Override
    public ViewGoods specChange(String specValues, int gid) {
        ViewGoods viewGoods=new ViewGoods();
        Map<String,List<String>> map= change(viewGoods,gid);
        viewGoods.setPurities(map.get("spec"));
        viewGoods.setId(goodsSpecMapper.getByJson(specValues,gid).getId());
        viewGoods.setPrice(goodsSpecMapper.getByJson(specValues,gid).getPrice());
        return  viewGoods;
    }

    @Override
    public ViewGoods purityChange(String specValues, int gid) {
        ViewGoods viewGoods=new ViewGoods();
        viewGoods.setId(goodsSpecMapper.getByJson(specValues,gid).getId());
        viewGoods.setPrice(goodsSpecMapper.getByJson(specValues,gid).getPrice());
        return viewGoods;
    }

    @Override
    public List<Goods> getByType(String type) {
        return viewGoodsMapper.getByType(type);
    }
}
